//
//  AppDelegate.m
//  Focus
//
//  Created by xwc on 14-10-27.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "subscribeController.h"
#import "recommendController.h"
#import "mineController.h"
#import "discoverController.h"
#import <ShareSDK/ShareSDK.h>
#import "WeiboSDK.h"
#import "WeiboApi.h"
#import "Reachability.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{
    [ShareSDK registerApp:@"api2"];
    
    //添加新浪微博应用
    [ShareSDK connectSinaWeiboWithAppKey:@"601323749"
                               appSecret:@"befc9c1cdcdd0a2b2f245ed9dbfe4b9e"
                             redirectUri:@"http://www.sharesdk.cn"];
    //添加腾讯微博应用
    [ShareSDK connectTencentWeiboWithAppKey:@"801307650"
                                  appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"
                                redirectUri:@"http://www.sharesdk.cn"
                                   wbApiCls:[WeiboApi class]];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    //网络变化判断
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    self.hostReach = [[Reachability reachabilityWithHostName:@"www.baidu.com"]retain];
    [self.hostReach startNotifier];

    
    
    [self.window makeKeyAndVisible];
    
    subscribeController *sub = [[subscribeController alloc]init];
    recommendController *recommend = [[recommendController alloc]init];
    discoverController *dis = [[discoverController alloc]init];
    //    mineController *mine = [[mineController alloc]init];
    
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:sub];
    UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:recommend];
    UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:dis];
    //    UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:mine];
    
    UIImage *imageOne = [[UIImage imageNamed:@"DashboardTabBarItemSubscription.png"]autorelease];
    imageOne = [imageOne imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    sub.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"订阅" image:imageOne tag:0];
    UIImage *imageTwo = [[UIImage imageNamed:@"DashboardTabBarItemDailyHot.png"]autorelease];
    imageTwo = [imageTwo imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    recommend.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"推荐" image:imageTwo tag:1];
    
    UIImage *imageThree = [[UIImage imageNamed:@"DashboardTabBarItemExplore.png"]autorelease];
    imageThree = [imageThree imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    dis.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"发现" image:imageThree tag:2];
//        UIImage *imageFour = [UIImage imageNamed:@"DashboardTabBarItemProfile.png"];
//        imageFour = [imageFour imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//        disAdd.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:imageFour tag:3];
    
    
    NSArray *array = [[[NSArray alloc]initWithObjects:nav1,nav2,nav3, nil]autorelease];
    UITabBarController *tab = [[UITabBarController alloc]init];
    tab.delegate  = self;
    tab.viewControllers = array;
    [_window setRootViewController:tab];
    
    [_window release];
    //添加动画
    self.splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 568)];
    self.splashView.image = [UIImage imageNamed:@"fengjing.jpeg"];
    [self.window addSubview:_splashView];
    [self.window bringSubviewToFront:_splashView];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:5.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionNone forView: self.window cache:YES];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
    _splashView.alpha = 0;
    _splashView.frame = CGRectMake(0, 0, 320, 635);
    //(-60, -85, 440, 635)

    [UIView commitAnimations];
    [_splashView release];
    [nav1 release];
    [nav2 release];
    [nav3 release];
    [sub release];
    [dis release];
    [recommend release];
    return YES;
}

- (void)dealloc
{
    [_window release];
    [super dealloc];
}

//动画方法
- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    [_splashView removeFromSuperview];
    [_splashView release];
}
//判断连接改变
- (void)reachabilityChanged:(NSNotification *)note
{
    Reachability *preachability = [note object];
    NSParameterAssert([preachability isKindOfClass:[Reachability class]]);
    NSString *pStr_3G =@"当前网络为2G或3G,3G已连接";
    NSString *pStr_WiFi =@"当前网络为Wi-Fi,Wi-Fi已连接";
    //NSString *pStr_WLAN =@"当前网络为WLAN";
    NSString *pStr_NO = @"当前网络已经断开";
    switch ([preachability currentReachabilityStatus]) {
        case NotReachable:
            [self alertShow:pStr_NO];
            break;
        case ReachableViaWiFi:
            [self alertShow:pStr_WiFi];
            break;
        case ReachableViaWWAN:
            [self alertShow:pStr_3G];
            break;
            //   case ReachableViaWWAN:
            //       [self alertShow:pStr_WLAN];
            //
        default:
            break;
    }
}
//提示框
- (void)alertShow:(NSString *)mes
{
    UIAlertView *AlertView = [[UIAlertView alloc]initWithTitle:@"通知" message:mes delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
    [AlertView show];
    [AlertView release];
    
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
